package cn.enilu.flash.service.api.express.yw;

import cn.enilu.flash.bean.entity.shop.ExpressInfo;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.exception.ApplicationExceptionEnum;
import cn.enilu.flash.service.api.express.ExpressApi;
import cn.enilu.flash.service.api.express.yw.bean.GetChannelResultDTO;
import cn.enilu.flash.service.api.express.yw.bean.YWExpressTrackingResultDTO;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.qcloud.cos.utils.XmlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @author qiang.lin
 * @ClassName YWService.java
 * @Description 燕文快递
 * @createTime 2021年08月10日 18:46:00
 */
@Primary
@Service(value = YWService.BEAN_NAME)
public class YWService implements ExpressApi {
    private Logger logger = LoggerFactory.getLogger(YWService.class);

    @Value("${express.yanwen.host}")
    private String yanwenUrl;

    @Value("${express.yanwen.userId}")
    private String yanwenUserId;

    @Value("${express.yanwen.auth}")
    private String yanwenAuth;

    public final static String BEAN_NAME = "ywService";

    //获取发货渠道
    private String EXPRESS_CHANNEL_INFO_URL = "/Users/{}/GetChannels";

    private String EXPRESS_CHANNEL_SEARCH_URL = "/USERS/{}/EXPRESSES?PAGE=1&CODE={}&RECEIVER=&CHANNEL=&START=2000-01-22 00:00:00&END={}&ISSTATUS=1";

    private String EXPREXX_TRACKING_URL = "http://trackapi.yanwentech.com/api/tracking";

    @PostConstruct
    public void init() {
        EXPRESS_CHANNEL_INFO_URL = yanwenUrl + EXPRESS_CHANNEL_INFO_URL;

        EXPRESS_CHANNEL_SEARCH_URL = yanwenUrl + EXPRESS_CHANNEL_SEARCH_URL;
    }

    @Override
    public ExpressInfo realTimeQuery(String orderNo, String companyCode) {
        HttpRequest getRequest = HttpUtil.createGet(EXPREXX_TRACKING_URL);
        wrapperRequestHeader(getRequest);
        HttpResponse getResponse = getRequest.execute();
        YWExpressTrackingResultDTO trackingResultDTO = JSONUtil.toBean(getResponse.body(), YWExpressTrackingResultDTO.class);
        ExpressInfo expressInfo = new ExpressInfo();
        if (trackingResultDTO == null) {
            return expressInfo;
        }
        expressInfo.setInfo(JSONUtil.toJsonStr(trackingResultDTO));
        expressInfo.setExpressCompany(companyCode);
        expressInfo.setOrderSn(orderNo);
        YWExpressTrackingResultDTO.TrackingInfo trackingInfo = trackingResultDTO.getResult().get(0);
        expressInfo.setShippingSn(trackingInfo.getTracking_number());
        expressInfo.setState(trackingInfo.getTracking_status());
        return expressInfo;
    }


    /**
     * 包装请求头
     *
     * @param httpRequest
     */
    private void wrapperRequestHeader(HttpRequest httpRequest) {
        Map<String, List<String>> headers = new HashMap<>();
        //headers.put("Authorization", ListUtil.list(true, "basic " + yanwenAuth));
        headers.put("Authorization", ListUtil.list(true, yanwenUserId));
        headers.put("Content-Type", ListUtil.list(true, "text/xml; charset=utf-8"));
        headers.put("Accept", ListUtil.list(true, "application/xml"));
        httpRequest.header(headers, true);
    }

    public static void main(String[] args) {

        /*HttpRequest getRequest2 = HttpUtil.createGet("http://online.yw56.com.cn/service/Users/20095963/Expresses?page=1&code=&receiver=&channel=&start=2000-01-22 00:00:00&end=2021-08-11 00:00:00&isstatus=1");
        Map<String, List<String>> headers = new HashMap<>();
        headers.put("Authorization", ListUtil.list(true, "basic 0C0630747D99F3F6FEB7537EB7EC749B"));
        headers.put("Content-Type", ListUtil.list(true, "text/xml; charset=utf-8"));
        headers.put("Accept", ListUtil.list(true, "application/xml"));
        getRequest2.header(headers, true);
        HttpResponse getResponse2 = getRequest2.execute();
        System.out.println("获取到的结果平时：{}" + getResponse2.body());*/
        HttpRequest getRequest = HttpUtil.createGet("http://trackapi.yanwentech.com/api/tracking?nums=UD512415397YP");
        Map<String, List<String>> headers = new HashMap<>();
        //headers.put("Authorization", ListUtil.list(true, "basic " + yanwenAuth));
        headers.put("Authorization", ListUtil.list(true, "20095963"));
        headers.put("Content-Type", ListUtil.list(true, "text/xml; charset=utf-8"));
        headers.put("Accept", ListUtil.list(true, "application/xml"));
        getRequest.header(headers, true);
        HttpResponse getResponse = getRequest.execute();
        YWExpressTrackingResultDTO trackingResultDTO = JSONUtil.toBean(getResponse.body(), YWExpressTrackingResultDTO.class);
        System.out.println(trackingResultDTO.getResult());
    }
}
